<?php

namespace Cyfy\Modules\Objects;

/**
 * Class for creating tables
 */
class Table extends GeneralObject
{
    /**
     * Table rows
     * @var array
     */
    private $rows = array();
    
    /**
     * Table header colums
     * @var array
     */
    private $headers=array();

    /**
     * Table columns classes;
     * @var array
     */
    private $clses=array();

    /**
     * Creates a new table
     * @param string Classname
     * @param string ID
     */
    function __construct($cls = '', $id = '')
    {
        $this -> cls = $cls;
        $this -> id = $id;
    }
    
    /**
     * Inserts a new header
     * @param array Columns.
     * @return bool
     */
    function header($row)
    {
        if(is_array($row)){
            $this -> headers[] = $row;
            $this -> classes = array_keys($row);
            return true;
        }
        return false;
    }
    
    /**
     * Inserts a new header.
     *
     * @param array Columns
     * @param string JS callback, when clicked
     * @param string Highlight, when mouse over?
     * @param string Custom ID
     * @param string Custom class
     * @return bool
     */
    function row($row, $onclick = "", $highlight = false, $id = "", $cls="")
    {		
        if(is_array($row)){
            $this -> rows[] = array("type" => "td", "data"=>$row, "id"=>$id, "onclick"=>$onclick, "highlight"=>$highlight, "class"=>$cls);
            return true;
        }
        return false;
    }
    
    /**
     * Sets new classes for columns. Starts with 0 for cells in first column, 1 for cells in second column...
     * @param array
     */
    function classes($row)
    {
        $this -> classes = $row;
    }
    
    /**
     * Returns table as HTML string
     * @return string HTML code
     */
    function show(){
        $output = "\n<table id=\"{$this -> id}\" class=\"{$this -> cls}\">\n";
        
        // ths		
        foreach($this -> headers as $r)
        {
            $output .= "	<thead>\n";
            $output .= "	  <tr class=\"nodrop nodrag\">\n";
            foreach($r as $h){
                $output .= "		  <th>{$h}</th>\n";
            }
            $output .= "	  </tr>\n";
            $output .= "	</thead>\n";
        }
    
        // tds		
        $output .= "	<tbody>\n";
        foreach($this -> rows as $r){
            $props = "";
            $clses = "";
            if($r["onclick"])
                $props .= " onclick=\"{$r["onclick"]}\"";
            if($r["highlight"])
                $clses .= "table-row-highlight";
            if($r["class"])
                $clses .= $r["class"];
            if($r["id"])
                $props .= " id=\"{$r["id"]}\"";
            
            $output .= "	  <tr class=\"{$clses}\"{$props}>\n";
            
            foreach($r["data"] as $i => $c){
                $cls = $this -> classes[$i];
                $output .= "		  <{$r["type"]} class=\"{$cls}\">{$c}</{$r["type"]}>\n";
            }
            $output .= "	  </tr>\n";
        }
        $output .= "	</tbody>\n";
        $output .= "</table>\n\n";
        
        if($r["highlight"])
            $output .= \Cyfy\Cyfy ::  javaScript("$(\".table-row-highlight\").hover(
                function(){
                    $(this).addClass(\"table-row-highlight-active\");
                },
                function(){
                    $(this).removeClass(\"table-row-highlight-active\");
                }
            );
        ");
        return $output;
    }
}